'''A general purpose library for emesene. This will help adding
actions to your plugin'''

import Plugin

class Actions:
    def __init__(self, controller, msn):
        self.controller = controller
        self.msn = msn

    def get_nick(self, contact=None):
        '''return the nick of contact. If contact is None, returns our nick'''
        return self.controller.contacts.get_nick(user)

    def set_nick(self, nick):
        '''set our nick to nick'''
        self.controller.contacts.set_nick(nick)

    def get_psm(self, contact=None):
        '''return the psm of contact. If contact is None, returns our psm'''
        contact = self.msn.contactManager.getContact(user)
        return contact.personalMessage

    def set_psm(self, psm):
        '''set our psm to psm'''
        self.controller.contacts.set_message(message)

    def get_picture_path(self, contact=None):
        '''return the picture_path of contact. If contact is None, returns our picture_path'''
        if contact:
            contact = self.msn.contactManager.getContact(user)
            picture = contact.displayPicturePath
            if picture:
                return os.path.join(
                        self.controller.config.getCachePath(), picture)
            else:
                return None
        else:
            return self.controller.avatar.getImagePath()

    def set_picture_path(self, picture_path):
        '''set our picture_path to picture_path'''
        self.controller.changeAvatar(picture_path)

    def send_nudge(self, conversation):
        '''send a nudge in conversation'''
        conversation.doNudge()

    def send_file(self, file, conversation):
        '''send a file in a conversation'''
        #TODO
        pass

    def get_conversation_user(self, contact):
        '''return the conversation in which there is ONLY contact, or None if not found.'''
        #TODO
        pass

class MacroManager:
    def __init__(self, actions):
        self.actions = actions #this is an instance of the actions class, ready to be used
        self.dictionary = {} #this is where we'll put the data
    
    def get_macro(self, macroName):
        if macroName in self.dictionary:
            return self.dictionary[macroName]
            
    def add_macro(self, actionlist):
        self.dictionary[actionlist[0]] = actionlist[1:]
    
    def remove_macro(self,macroName):
        self.dictionary.pop(macroName)
    
    def editAction(self, macroName, actionNumber, newaction):
        (self.dictionary[macroName])[actionNumber] = newaction

    def run_macro(self, macro):
        for id, param in macro:
            if id == 'sendfile':
                pass
            if id == 'nudge':
                pass
            if id == 'set_nick':
                pass

def MainClass(Plugin.Plugin):
    '''Main plugin class'''
    def __init__(self, controller, msn):
        Plugin.Plugin.__init__(self, controller, msn)
        self.description = _('Library for Plugin devs')
        self.authors = {'BoySka' : 'boyska gmail com'.
                'Otacon' : 'otacon katamail com'}
        self.website = 'http://emesene-plugins.googlecode.com'
        self.displayName = _('Plugin Library')
        self.name = 'PluginLibrary'

        self.controller = controller
        self.msn = msn

        self.actions = Actions(controller, msn)
        self.macros = MacroManager(self.actions)
